package weibo;

import utils.JsonOut;
import utils.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

@WebServlet(name = "HotWeiboServlet")
public class HotWeiboServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String fromNum = request.getParameter("m");
        String toNum = request.getParameter("n");

        if (fromNum == null || fromNum.isEmpty()) {
            fromNum = "0";
        }

        if (toNum == null || toNum.isEmpty()){
            toNum = "10";
        }

        int fromInt = 0;
        int toInt = 10;

        try{
            fromInt = Integer.parseInt(fromNum);
            toInt = Integer.parseInt(toNum);
            if (toInt < fromInt) toInt = fromInt;
        }catch (Exception e){
            e.printStackTrace();
        }

        ResultSet ret = (new MysqlUtils()).hotWeibo(fromInt, toInt);
        try {
            ArrayList<HashMap<String, String>> array = new ArrayList<>();
            while (ret.next()){
                HashMap<String, String> item = new HashMap<>();

                // blog.id,blog.user_id,nickname,head_image,blog.content,blog.time,like_num,comment_num,reblog_user,reblog_content,reblog_time
//                "b2.user_id as reblog_user,u2.nickname as reblog_nickname,u2.head_image as reblog_headimg,b2.content as reblog_content,b2.time as reblog_time " +

                //blog.id,blog.user_id,user.nickname, user.head_image,blog.content,blog.time,
                // blog.reblog_id,b2.user_id,u2.nickname,u2.head_image,b2.content,b2.time
                String blogId = ret.getString(1);
                String userId = ret.getString(2);
                String nickname = ret.getString(3);
                String headImage = ret.getString(4);
                String content = ret.getString(5);
                String time = ret.getString(6);
//                String likeNum = ret.getString(7);
//                String commentNum = ret.getString(8);

                String reblogId = ret.getString(7);
                String reblogUserId = ret.getString(8);
                String reblogNickname = ret.getString(9);
                String reblogHeadImg = ret.getString(10);
                String reblogContent = ret.getString(11);
                String reblogTime = ret.getString(12);



                item.put("blog_id", blogId != null ? blogId : "");
                item.put("user_id", userId != null ? userId : "");
                item.put("nickname", nickname != null ? nickname : "");
                item.put("head_image", headImage != null ? headImage : "");
                item.put("content", content != null ? content : "");
                item.put("time", time != null ? time : "");
//                item.put("like_num", likeNum != null ? likeNum : "");
//                item.put("comment_num", commentNum != null ? commentNum : "");
                item.put("reblog_id", reblogId != null ? reblogId : "");
                item.put("reblog_user_id", reblogUserId != null ? reblogUserId : "");
                item.put("reblog_nickname", reblogNickname != null ? reblogNickname : "");
                item.put("reblog_head_image", reblogHeadImg != null ? reblogHeadImg : "");
                item.put("reblog_content", reblogContent != null ? reblogContent : "");
                item.put("reblog_time", reblogTime != null ? reblogTime : "");

                String image = ret.getString(13);
                String reblogImage = ret.getString(14);
                item.put("image", image != null ? image : "");
                item.put("reblog_image", reblogImage != null ? reblogImage : "");

//                int reblogNum = (new MysqlUtils()).getWeiboReblogNum(blogId);
//                item.put("reblog_num", String.valueOf(reblogNum));

                String likeNum = (new MysqlUtils()).weiboLikeNum(blogId);
                item.put("like_num", likeNum != null ? likeNum : "");

                // can_delete判断是否能删除
                item.put("can_delete", "0");
                // liked判断是否赞了
                item.put("liked", "0");

                array.add(item);
            }

            out.println(JsonOut.common(0, "blog_list", array));
        } catch (Exception e) {
            e.printStackTrace();
            out.println(JsonOut.error(500, "sql_sth_wrong"+e.toString()));
        }
    }
}
